#! /bin/bash

# Name of root dir and repository
PROJECTNAME="inasafe"
InaSAFEDIR="$PROJECTNAME-dev"   # Master advertises as inasafe-dev
InaSAFEREPO="AIFDR/$PROJECTNAME.git"

echo "---------------------------------------------------------"
echo "Installing InaSAFE and all its dependencies."
echo "This will take a little while depending on"
echo "the internet speed."
echo
echo "In any case, now is great time to go get a cup of coffee!"
echo "---------------------------------------------------------"

echo "Installing Ubuntu packages for running InaSAFE"
gpg --keyserver keyserver.ubuntu.com --recv 1F9ADD375CA44993
gpg --export --armor 1F9ADD375CA44993 | sudo apt-key add -
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:ubuntugis/ubuntugis-unstable
# for python reindent helper
sudo apt-get install python2.7-examples
sudo apt-get -qq update
sudo apt-get install -q -y qgis python-qgis

echo "Installing Ubuntu packages for development"
sudo apt-get install -y git rsync pep8 python-nose python-coverage python-numpy\
     pyqt4-dev-tools python-pip libqt4-dev pyflakes python-nosexcover python-scientific\
     python-pytz python-beautifulsoup fabric
echo "Installing sphinx from pip because at least 1.1 is needed for i18n support"
echo "Installing specific pylint version (0.25) for cross host comparison"
sudo pip install sphinx pylint==0.25.2
echo "Installing transifex web translation client"
sudo pip install transifex-client python-transifex
# For code metrics / line counts
sudo apt-get install sloccount
# For gettext translation, translation statistics
sudo apt-get install gettext
# For tex2pdf command
sudo apt-get install texinfo

# Setup environment variable.
if grep -q InaSAFEQGIS ~/.bash_aliases
then
    echo "You already have InaSAFEQGIS in your ~/.bash_aliases"
    source ~/.bash_aliases
else
    export InaSAFEQGIS=`pwd`/$InaSAFEDIR  # Make path available for this session

    echo Path to InaSAFEQGIS is $InaSAFEQGIS

    # Store it for future sessions
    echo "export InaSAFEQGIS=$InaSAFEQGIS" >> ~/.bash_aliases
    echo "Adding InaSAFEQGIS path to your ~/.bash_aliases"
 fi

echo "InaSAFEQGIS:" $InaSAFEQGIS

# Function to clone the git repositories
function checkup() {
  REPO="$1"
  WORKING_DIR="$2"
  BRANCH="$3"
  if [ -d "${WORKING_DIR}" ];
  then
      echo "Updating ${WORKING_DIR} from upstream"
      (cd "${WORKING_DIR}" && git pull)
  else
      echo "git@github.com:${REPO} ${WORKING_DIR}"
      echo "https://github.com/${REPO} ${WORKING_DIR}"
      echo "http://github.com/${REPO} ${WORKING_DIR}"
      git clone "git@github.com:${REPO}" "${WORKING_DIR}" || git clone "https://github.com/${REPO}" "${WORKING_DIR}" || git clone "http://github.com/${REPO}" "${WORKING_DIR}"
  fi
  git submodule init
  git submodule update

  pushd .
  cd $WORKING_DIR
  git fetch origin $BRANCH
  git checkout $BRANCH
  popd
}

echo "Downloading InaSAFE"
checkup ${InaSAFEREPO} ${InaSAFEDIR} master

echo "Create symlink $InaSAFEQGIS from Qgis plugin area"
chown $USER ~/.qgis2
mkdir ~/.qgis2/python
mkdir ~/.qgis2/python/plugins
pushd ~/.qgis2/python/plugins
ln -sf $InaSAFEQGIS
popd

echo "Downloading test and demo data"
echo dir $InaSAFEQGIS
pushd $InaSAFEQGIS
make testdata
popd


echo
echo "Congratulations, you have installed InaSAFE"
echo
echo "To verify that the installation works, you can run the test suite"
echo "cd $InaSAFEQGIS"
echo "make test"
